package com.xbxkj.commen.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xbxkj.commen.bean.UserAddress;
import com.xbxkj.commen.controller.helper.$;
import com.xbxkj.commen.controller.model.ApiResultModel;
import com.xbxkj.commen.mapper.UserAddressMapper;
import com.xbxkj.commen.service.UserAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbxkj.commen.bean.layuiBean.ResultPage;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张子艺
 * @since 2020-01-02
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {
    /** 添加UserAddress
     * @param userAddress  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> addUserAddress(UserAddress userAddress  ) {
        userAddress.setIsDefault(2);
           if(this.save(userAddress )){
               return $.success();
           }else{
                return  $.error("添加失败");
           }
    }
    /** 编辑跳转页面的回显数据UserAddress
     * @param id  前端传进的参数
     * @return 返回实体类
     */
    @Override
    public UserAddress selectUserAddressByIdUserAddress(String id ){
            return this.getById(id);
    }


    /** 编辑UserAddress
     * @param userAddress  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel<Object> editUserAddress(UserAddress userAddress  ) throws Exception {
        //如果修改的是 默认地址  那么先把该用户所有地址 设置为不默认
            if(userAddress.getIsDefault()==1){
                UpdateWrapper updateWrapper=new UpdateWrapper();
                updateWrapper.eq("user_id",userAddress.getUserId());
                updateWrapper.set("is_default",2);
                update(updateWrapper);
            }
           if(this.updateById(userAddress )){
               return  $.success();
           }else{
               throw  new Exception("修改失败，不存在该收货地址");
           }
    }
    /** 删除一条UserAddress
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public   ApiResultModel<Object> delUserAddress( String id  ){
           if(this.removeById(id)){
               return $.success();
           }else{
                return $.error("删除失败");
           }
    }

   /** 分页查询UserAddress
     * @param page  页码
     * @param limit  查几条
     * @param userAddress  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public  ResultPage findUserAddressByPage(Integer page, Integer limit,UserAddress userAddress){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("userAddress", userAddress);
        List<UserAddress> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<UserAddress> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
}
